package model

import (
	"fmt"

	"gitee.com/dn-jinmin/chatcms/internal/domain"
	"github.com/tmc/langchaingo/schema"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

type Speechcraft struct {
	ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
	// TODO: Fill your own fields

	Key   string `bson:"key,omitempty" json:"key,omitempty"`
	Value string `bson:"value,omitempty" json:"value,omitempty"`

	UpdateAt int64 `bson:"updateAt,omitempty" json:"updateAt,omitempty"`
	CreateAt int64 `bson:"createAt,omitempty" json:"createAt,omitempty"`
}

func (s *Speechcraft) ToDomain() *domain.Speechcraft {
	return &domain.Speechcraft{
		Id:  s.ID.Hex(),
		Key: s.Key,
		Val: s.Value,
	}
}

func SpeechcraftFromDomain(d *domain.Speechcraft) *Speechcraft {
	oid, err := primitive.ObjectIDFromHex(d.Id)
	if err != nil {
		oid = primitive.NewObjectID()
	}

	return &Speechcraft{
		ID:    oid,
		Key:   d.Key,
		Value: d.Val,
	}
}

func SpeechcraftsToDocument(list []*Speechcraft) []schema.Document {
	res := make([]schema.Document, 0, len(list))
	for _, speechcraft := range list {
		res = append(res, schema.Document{
			PageContent: fmt.Sprintf("问题:%s, 话术:%s", speechcraft.Key, speechcraft.Value),
			Metadata:    map[string]any{},
			Score:       0,
		})
	}
	return res
}
